//
//  ExchangePassword.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/29.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ExchangePassword.h"
#import "BaseLabel.h"
#import "BaseTextField.h"
#import "BaseButton.h"
#import "AFNetworking.h"
#import "NSString+HVV.h"
#import "Masonry.h"
@interface ExchangePassword ()
@property (nonatomic, retain) BaseLabel *labelOldPassword;
@property (nonatomic, retain) BaseTextField *textFieledOfOldPassword;
@property (nonatomic, retain) BaseLabel *labelOfNewPassword;
@property (nonatomic, retain) BaseTextField *textFieldOfNewPassword;
@property (nonatomic, retain) BaseLabel *labelOfConfirmNewPassWord;
@property (nonatomic, retain) BaseTextField *textFieldOfConfirmNewPassword;
@property (nonatomic, retain) NSString *uid;
@property (nonatomic, retain) NSString *cookie;
@property (nonatomic, assign) NSNumber *statusCode;
@property (nonatomic, retain) NSURL *urlOfCookie;



@property (nonatomic, retain) BaseButton *buttonOfExchangePassword;
@end

@implementation ExchangePassword

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createView];


}

#pragma mark - 创建本页基础控件

- (void)createView{
    
    
    self.labelOldPassword = [[BaseLabel alloc]init];
   self.labelOldPassword.text = @"  当前密码:";
    [self.labelOldPassword.layer setMasksToBounds:YES];
    [self.labelOldPassword.layer setCornerRadius:5];
    [self.view addSubview:self.labelOldPassword];
    [self.labelOldPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(74);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.24);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    
    self.textFieledOfOldPassword = [[BaseTextField alloc]init];
    self.textFieledOfOldPassword.borderStyle = UITextBorderStyleRoundedRect;
    self.textFieledOfOldPassword.clearsOnInsertion = YES;
    self.textFieledOfOldPassword.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.textFieledOfOldPassword];
    [self.textFieledOfOldPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.labelOldPassword.mas_right).mas_offset(10);
        make.top.mas_equalTo(74);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    

     self.labelOfNewPassword = [[BaseLabel alloc]init];
     self.labelOfNewPassword.text = @"    新密码:";
    [self.labelOfNewPassword.layer setMasksToBounds:YES];
    [self.labelOfNewPassword.layer setCornerRadius:5];
    [self.view addSubview: self.labelOfNewPassword];
    [self.labelOfNewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.labelOldPassword.mas_bottom).mas_offset(20);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.24);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];

    
    self.textFieldOfNewPassword = [[BaseTextField alloc]init];
    self.textFieldOfNewPassword.borderStyle = UITextBorderStyleRoundedRect;
    self.textFieldOfNewPassword.placeholder = @"密码为6 - 16位字母或数字";
    self.textFieldOfNewPassword.clearsOnInsertion = YES;
    self.textFieldOfNewPassword.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.textFieldOfNewPassword];
    [self.textFieldOfNewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.textFieledOfOldPassword);
        make.centerY.mas_equalTo(self.labelOfNewPassword);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    

    self.labelOfConfirmNewPassWord = [[BaseLabel alloc]init];
    self.labelOfConfirmNewPassWord.text = @"确认新密码:";
    [self.labelOfConfirmNewPassWord.layer setMasksToBounds:YES];
    [self.labelOfConfirmNewPassWord.layer setCornerRadius:5];
    [self.view addSubview:self.labelOfConfirmNewPassWord];
    [self.labelOfConfirmNewPassWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.labelOldPassword.mas_width);
        make.height.mas_equalTo(self.labelOldPassword.mas_height);
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.labelOfNewPassword.mas_bottom).mas_offset(20);
    }];


    self.textFieldOfConfirmNewPassword = [[BaseTextField alloc]init];
    self.textFieldOfConfirmNewPassword.borderStyle = UITextBorderStyleRoundedRect;
    self.textFieldOfConfirmNewPassword.placeholder = @"再次确认密码";
    self.textFieldOfConfirmNewPassword.clearsOnInsertion = YES;
    self.textFieldOfConfirmNewPassword.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.textFieldOfConfirmNewPassword];
    [self.textFieldOfConfirmNewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.textFieldOfNewPassword);
        make.centerY.mas_equalTo(self.labelOfConfirmNewPassWord);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
    }];
    
    
    self.buttonOfExchangePassword = [BaseButton buttonWithType:UIButtonTypeCustom];
    [self.buttonOfExchangePassword setTitle:@"修改密码" forState: UIControlStateNormal];
    self.buttonOfExchangePassword.backgroundColor = [UIColor colorWithRed:18.0 / 255 green:86.0 / 255 blue:138.0 / 255 alpha:1];
    [self.view addSubview:self.buttonOfExchangePassword];
    [self.buttonOfExchangePassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.08);
        make.centerY.mas_equalTo(self.view.mas_centerY).mas_offset(70);
        make.centerX.mas_equalTo(self.view);
    }];
    
    [self.buttonOfExchangePassword addTarget:self action:@selector(exchangePassword:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)exchangePassword:(BaseButton *)button{

   
    if (self.textFieledOfOldPassword.text.length < 6) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"当前密码输入有误" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"好的,我重新输入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //按钮触发的方法
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    } else if (self.textFieldOfNewPassword.text.length < 6  || self.textFieldOfNewPassword.text.length >16){
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"新密码不符合要求" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"好的,我重新输入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //按钮触发的方法
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];
    
    }else if (self.textFieldOfConfirmNewPassword.text.length < 6 || self.textFieldOfConfirmNewPassword.text.length >16){
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认密码输入不符合要求" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"好的,我重新输入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //按钮触发的方法
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];
    
    
    }else if ([self.textFieldOfNewPassword.text isEqual:self.textFieledOfOldPassword.text]){
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"新密码和旧密码一样呢" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"好的,我重新输入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //按钮触发的方法
        }]];
        
        [self presentViewController:alert animated:YES completion:nil];

    
    
    }else if ([self.textFieldOfNewPassword.text isEqual:self.textFieldOfConfirmNewPassword.text]){
    
        //将获取到的Cookie本地化
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *pathUid = [[path lastObject]stringByAppendingPathComponent:@"uid.TXT"];
        self.uid = [NSString stringWithContentsOfFile:pathUid encoding:NSUTF8StringEncoding error:nil];
        NSString *pathCookie = [[path lastObject]stringByAppendingPathComponent:@"cookie.TXT"];
        self.cookie = [NSString stringWithContentsOfFile:pathCookie encoding:NSUTF8StringEncoding error:nil];
        self.cookie = [self.cookie stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        
        //判断输入符合要求后,发更改密码请求;
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *postURLstring = @"http://app.yanyue.cn/user/changepass";
        
        NSDictionary *parmeters = @{@"currentPassword":self.textFieledOfOldPassword.text,@"newPassword":self.textFieldOfNewPassword.text,@"uid":self.uid,@"cookie":self.cookie};
       
        NSLog(@"%@",parmeters);
        
        [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
        [set addObject:@"text/html"];
        [manager.responseSerializer setAcceptableContentTypes:set];
        
        // 设置请求POST发送
        [manager POST:postURLstring parameters:parmeters progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            self.statusCode = responseObject[@"status"];
            
            /*
             -2 cookie错误;
             1  成功
             0  参数错误
             -1 当前密码错误
             
             */
            
            NSLog(@"%@",responseObject);
            
            if ([self.statusCode integerValue] == 1) {
                
                
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"修改成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                
                [self presentViewController:alert animated:YES completion:nil];
                
            }else if ([self.statusCode integerValue] == 0){
            
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"登录超时请再次尝试" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                
                [self presentViewController:alert animated:YES completion:nil];
            
            
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败: %@", error);
        }];
        

        
        
        
    
    }else {
    
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"新密码和确认密码不一致" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"好的,我重新输入" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //按钮触发的方法
        }]];

        [self presentViewController:alert animated:YES completion:nil];

    
    }
    


}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
